package cn.maihe.elg.operation.supports.bank;

import cn.maihe.elg.operation.config.CcbBankConfig;
import cn.maihe.elg.operation.model.dto.BaseReq;
import cn.maihe.elg.operation.model.dto.BaseResp;
import cn.maihe.elg.operation.supports.bank.ccb.ebs.client.CCBEbsPayRequestClient;
import cn.maihe.elg.operation.supports.bank.ccb.ebs.dto.CCBEbsTransDetailQueryReq;
import cn.maihe.elg.operation.supports.bank.ccb.ebs.dto.CCBEbsTransDetailQueryResp;
import cn.maihe.elg.operation.supports.bank.ccb.ebs.dto.CCBEbsTransDetailXmlResp;
import cn.maihe.elg.operation.supports.bank.dto.AccountDetailRespDTO;
import cn.maihe.elg.operation.supports.bank.enums.CcbMethod;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import cn.maihe.elg.operation.utils.ElgLogger;
import cn.maihe.elg.operation.utils.XmlUtil;
import com.alibaba.fastjson.JSON;
import com.open.sdk.OpenSDK;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @Description 建设银行交易服务类
 * @Author Alan
 * @Date 2024/11/4
 */
@Service
@Slf4j
public abstract class BaseCcbBankTransService<REQ extends BaseReq, RESP extends BaseResp> {

    protected Class<REQ> reqClass;
    protected Class<RESP> respClass;

    @Resource
    protected CcbBankConfig ccbBankConfig;
    @Resource
    protected ElgResourceService elgResourceService;

    public BaseCcbBankTransService() {
        Type[] types = ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments();
        this.reqClass = (Class<REQ>) types[0];
        this.respClass = (Class<RESP>) types[1];
    }

    @PostConstruct
    public void init() {
    }

    protected abstract CcbMethod ccbMethod();

    public REQ toReq(Object obj) {
        return JSON.parseObject(JSON.toJSONBytes(obj), this.reqClass);
    }

    public CCBEbsTransDetailXmlResp queryTrans(CCBEbsTransDetailQueryReq req) {
//        String traceId = IdGenerate.genTranceId();
        CCBEbsTransDetailQueryResp resp = null;
        CCBEbsTransDetailXmlResp xmlResp = null;
        try {
//            String data = JSON.toJSONString(req.getClearBody());
//            CCBEbsPayQueryReq model = new CCBEbsPayQueryReq(ccbBankConfig.getCustId(), ccbBankConfig.getUserId(),
//                    ccbBankConfig.getUserPwd(), CcbMethod.Q6WY105.getMethodId(), ccbBankConfig.getAccNo(),null);
            String result = new CCBEbsPayRequestClient().queryTrans(req, ccbBankConfig.getHost(),
                    ccbBankConfig.getPort());
//            String response = OpenSDK.sendData(xibBankConfig.getPdId(), xibBankConfig.getApiId(), data);
            Assert.notNull(resp, () -> "建设银行响应报文为空");
            xmlResp = (CCBEbsTransDetailXmlResp) XmlUtil.xmlStrToObject(this.respClass, result.trim());
//            System.out.println("response===" + response);
//            accountDetailRespDTO = JSON.parseObject(response, XibAccountDetailRespDTO.class);
            String requestSn = xmlResp.getRequestSn();
            String respCode = xmlResp.getReturnCode();
            String respMsg = xmlResp.getReturnMsg();
            CCBEbsTransDetailQueryResp finalResp = resp;
            ElgLogger.info(log, l -> l.info("{} << {} 建设银行响应报文原文:{}", requestSn, ccbMethod(), finalResp));
            if (!"000000".equals(respCode)) {
                throw new RuntimeException("建设银行响应报文响应头,requestSn:" + requestSn + "respCode:" + respCode + " respMsg:" + respMsg);
            }
        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("{} << {} 请求响应异常:{}", req.getRequestSn(), ccbMethod(), e.getMessage(), e));
            throw new RuntimeException("处理异常:" + e.getMessage());
        }
        return xmlResp;
    }

    @Test
    public void openSDKTest() throws Exception {
        String privateKey = "ANMNzr5tmnUsufjET6yotnNKU/nAvzidG7OKoE6GaTcj";
        String path = this.getClass().getClassLoader().getResource("config/config-dev.properties").getPath();
        OpenSDK.init(path, privateKey);
        String pdId = "ZHFW0002";   //产品ID ZF]S0004
        String apiId = "OPXM0001A201000700000100";  //接口ID
        String data = "{\"pageNo\":1,\"transTimeEnd\":\"20240827212700\",\"pageSize\":50,\"custAcct\":\"8005100000017168\",\"transTimeStart\":\"20240827000000\"}";
        OpenSDK.sendData(pdId, apiId, data);
    }
}
